from functools import wraps
from sqlalchemy import create_engine
from sqlalchemy import text
from sqlalchemy.orm import Session

def flyweight(cls):
    _instance = {}

    def _make_arguments_to_key(args, kwds):
        key = args
        if kwds:
            sorted_items = sorted(kwds.items())
            for item in sorted_items:
                key += item
        return key

    @wraps(cls)
    def _flyweight(*args, **kwargs):
        cache_key = f'{cls}_{_make_arguments_to_key(args, kwargs)}'
        if cache_key not in _instance:
            _instance[cache_key] = cls(*args, **kwargs)
        return _instance[cache_key]

    return _flyweight

@flyweight
class DBTools:
    def __init__(self, url):
        self.url = url
        self.session = Session(create_engine(self.url))
    def connectDB(self):
        return self.session

if __name__ == '__main__':
    from common.config import commonConfig as Config
    # url = 'mysql+pymysql://root:123456@localhost:3306/eladmin?charset=utf8'
    url = Config.url
    columName = "preparePitchExploreData"
    value = 122
    session = DBTools(url).connectDB()
    # result = session.execute(
    #     text("UPDATE status SET value=:value WHERE name=:name"), [{"name": columName, "value": value}])

    result = session.execute(
        text("select * from data1"))
    print(result.all())
    session.commit()
    session.close()


    # sql ="""select name from df limit 2"""

